package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import common.result.ResultEnum;
import common.util.PageBean;
import mapper.DepartmentMapper;
import model.dto.DepartmentDto;
import model.dto.SysRoleDto;
import model.dto.SysUserDto;
import model.req.SysUserReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.SysRolesService;
import service.SysUsersService;

import javax.xml.ws.soap.Addressing;
import java.util.List;
import java.util.Map;

/**
 * @className: controller.UserController
 * @author: Xia
 * @create: 2023-03-14 10:35
 */
@Controller
@RequestMapping("api")
public class UserController {
    @Autowired
    private SysUsersService service;
    @Autowired
    private SysRolesService rolesService;
    @Autowired
    private DepartmentMapper mapper;

    @RequestMapping("/users/list")
    public String getUsersList(Integer userType,
                               String account,
                               String userName,
                               @RequestParam(name = "pageNo",defaultValue = "1") Integer index, Model model){
        PageBean<SysUserDto> page = service.page(userType, account, userName, index);
        model.addAttribute("pages",page);
        model.addAttribute("queryUserType",userType);
        model.addAttribute("queryAccount",account);
        model.addAttribute("queryUserName",userName);
        return "userList";
    }

    @RequestMapping("auth/getRoleList")
    @ResponseBody
    public String getRoleList(){
        List<SysRoleDto> rolesList = rolesService.getRolesList();
        return JSON.toJSONString(rolesList);
    }

    @RequestMapping("department/getDepartmentList")
    @ResponseBody
    public String getDepartmentList(Map<String, Object> param){
        List<DepartmentDto> departmentList = mapper.getDepartmentList(param);
        return JSON.toJSONString(departmentList);
    }

    @RequestMapping("users/addUser")
    public String toAdd(){
        return "userAdd";
    }

    @RequestMapping("users/accountExist")
    @ResponseBody
    public String getAccount(Long id){
        SysUserDto userById = service.getUserById(id);
        return JSON.toJSONString(userById);
    }

    @RequestMapping("userAdd")
    public String userAdd(SysUserReq sysUserReq,String account){
        System.out.println(account);
        Integer integer = service.addUser(null);
        if (integer == 1){
            return "redirect:users/list";
        }else {
            return "redirect:users/addUser";
        }
    }

    @RequestMapping("users/getDoctorList")
    @ResponseBody
    public String getDoctorList(Integer departmentId, String userName){
        List<SysUserDto> doctorList = service.getDoctorList(departmentId, userName);
        return JSON.toJSONString(doctorList);
    }

}
